<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;
use PHPMailer\PHPMailer\PHPMailer;
use think\Cache;

class Register extends Controller
{
    public function index()
    {
        // 注册页面
        return view('index@Register/index',[
            'title' => '注册用户页面'
        ]);
    }

     public function email(Request $request)
    {
        $emails = $request->post('email');
       // var_dump($emails);die;
        $random = rand(100000, 999999);
//         $keydate = time();
        //实例化
        $mail=new PHPMailer();

        try{
           //邮件调试模式
           $mail->SMTPDebug = 2;
           //设置邮件使用SMTP
           $mail->isSMTP();
           // 设置邮件程序以使用SMTP
           $mail->Host = ' smtp.163.com';
           // 设置邮件内容的编码
           $mail->CharSet='UTF-8';
           // 启用SMTP验证
           $mail->SMTPAuth = true;
           // SMTP username
           $mail->Username = '15021076332@163.com';
           // SMTP password
           $mail->Password = 'dihuazhixiu123';
           // 启用TLS加密，`ssl`也被接受
          $mail->SMTPSecure = 'ssl';
           // 连接的TCP端口
          $mail->Port = 465;
           //设置发件人
           $mail->setFrom('15021076332@163.com', 'whale');
          //  添加收件人1
           $mail->addAddress($emails);     // Add a recipient
//           $mail->addAddress('ellen@example.com');               // Name is optional
           // 收件人回复的邮箱
            $mail->addReplyTo('15021076332@163.com', 'whale');
           // 抄送
//           $mail->addCC('cc@example.com');
//           $mail->addBCC('bcc@example.com');
            // 附件
           // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
           // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
            // Content
            // 将电子邮件格式设置为HTML
            $mail->isHTML(true);
            $mail->Subject = '邮箱验证';
            $mail->Body    = "验证码: ".$random;
            $mail->send();
            Session::set('code', $random);
       }catch (Exception $e){

           echo 'Mailer Error: ' . $mail->ErrorInfo;
       }
    }

     public function cache()
    {
        // dump(session_save_path());die;
        if (!Cache::store('redis')->get('name')) {
            $data = date('Y-m-d H:i:s', time());
            Cache::store('redis')->set('name',$data,10);
        }
        $this->assign('time', Cache::store('redis')->get('name'));
        return $this->fetch('index/index');
    }

    public function insert(Request $request)
    {

        $res = $request->post();
        $code = Session::get('code');
        if($res['code'] != $code){
            $this->error('验证码输入错误，请重新输入ㄟ( ▔, ▔ )ㄏ (*´Д｀*) ', url('index/Register/index'));
        }
        if($res['password'] != $res['repassword']){
            $this->error('两次密码输入不一致，请重新输入 (｀д′) (￣^￣) ', url('index/Register/index'));
        }
        $str = db('h_user')->where('phone', $res['phone'])->find();

        if ($str > 0) {
            $this->error('该手机已被注册，请重新输入 (｀д′) (￣^￣) ', url('index/Register/index'));
        }
        $msg = db('h_user')->where('email', $res['email'])->find();
        if($msg > 0){
            $this->error('该邮箱已被注册，请重新输入 (｀д′) (￣^￣) ', url('index/Register/index'));
        }

        $data = [
            'phone'=>$res['phone'],
            'password'=>md5($res['password']),
            'name'=>$res['name'],
            'email'=>$res['email'],
            'createtime'=>time(),
            'status'=>'1'
        ];

        $list = db('h_user')
            ->insert($data);

        Session::set($str['id']);
        Session::set($str['name']);
        Session::set($str['email']);
        Session::set($str['phone']);

        return $this->redirect('index/Login/index');


//       $code = Session::get('code');
//         $res = $request->post();
//         // dump($res);die;
//         $mob = db('h_user')->where('email', $res['email'])->find();
// //        var_dump($code);die;
//         if($mob){
//             return false;
//         }
//         // var_dump($res);
//         if($res['code'] != $code){
//             return false;
//         }
//         if($res['password'] != $res['repassword']){
//            return false;
//         }

//         unset($code);
//         // $name = date('Ymd');
//         $data = [
//             'phone'=>$res['phone'],
//             'password'=>md5($res['password']),
//             'name'=>$res['name'],
//             'email'=>$res['email'],
//             'createtime'=>time(),
//             'status'=>'1'
//         ];

//         $list = db('h_user')
//             ->insert($data);
//         $str = db('h_user')->where('phone', $data['phone'])->find();
//         Session::set($str['id']);
//         Session::set($str['email']);
//         Session::set($str['phone']);

//         if ($data > 0) {
//             return $this->redirect('index/Login/index');
//         }else{
//             return $this->redirect('index/Register/index');
//         }
    }

}